/**
 * 管理初始化
 */
var Insurance = {
    id: "InsuranceTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Insurance.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '保险公司代码', field: 'insuranceCode', visible: true, align: 'center', valign: 'middle'},
            {title: '保险公司名称', field: 'insuranceName', visible: true, align: 'center', valign: 'middle'},
            {title: '保险公司简称', field: 'insuranceAbbr', visible: true, align: 'center', valign: 'middle'},
            {title: '社会信用代码', field: 'creditCode', visible: true, align: 'center', valign: 'middle'},
            {title: '附件个数', field: 'accessoryNum', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Insurance.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Insurance.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
Insurance.openAddInsurance = function () {
    var index = layer.open({
        type: 2,
        title: '添加保险公司',
        area: ['1000px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/insurance/insurance_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看详情
 */
Insurance.openInsuranceDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改保险公司',
            area: ['1000px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/insurance/insurance_update/' + Insurance.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
Insurance.delete = function () {
    if (this.check()) {
    	
    	var operation = function(){
    		var insuranceId = Insurance.seItem.id;
	        var ajax = new $ax(Feng.ctxPath + "/insurance/delete", function (data) {
	            Feng.success("删除成功!");
	            Insurance.table.refresh();
	        }, function (data) {
	            Feng.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("insuranceId",insuranceId);
	        ajax.start();
    	};
    	
    	Feng.confirm("是否删除保险公司" + this.seItem.insuranceName + "?",operation);	
    }
};

/**
 * 附件
 */
Insurance.openAccessory = function () {
    if (this.check()) {
		 var index = layer.open({
	         type: 2,
	         title: '附件管理',
	         area: ['800px', '420px'], //宽高
	         fix: false, //不固定
	         maxmin: true,
	         content: Feng.ctxPath + '/uploadFile/accessory/insurance/' + Insurance.seItem.id + '/150'
	     });
		 this.layerIndex = index;
    }
};

/**
 * 重置
 */
Insurance.resetSearch = function () {
    $("#condition").val("");
    
    Insurance.search();
};

/**
 * 查询列表
 */
Insurance.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Insurance.table.refresh({query: queryData});
};

/**
 * 将所输入的社会信用代码按照对应的文本格式处理X-X-XXXXXX-XXXXXXXXX-X后返回
 */
Insurance.resetCreditCode = function() {
	var newCreditCode = '';
	var creditCode = $('#creditCode').val().replace(/-/g,"");
	var creditCodeLength = creditCode.length;
	if(creditCodeLength>17){
		newCreditCode = creditCode.substr(0,1) + '-' + creditCode.substr(1,1) + '-' + creditCode.substr(2,6) + '-' + creditCode.substr(8,9) + '-' + creditCode.substr(17,1);
	}else if(creditCodeLength>8){
		newCreditCode = creditCode.substr(0,1) + '-' + creditCode.substr(1,1) + '-' + creditCode.substr(2,6) + '-' + creditCode.substr(8,9);
	}else if(creditCodeLength>2){
		newCreditCode = creditCode.substr(0,1) + '-' + creditCode.substr(1,1) + '-' + creditCode.substr(2,6);
	}else if(creditCodeLength>1){
		newCreditCode = creditCode.substr(0,1) + '-' + creditCode.substr(1,1);
	}else{
		newCreditCode = creditCode.substr(0,1);
	}
	return newCreditCode;
}

$(function () {
    var defaultColunms = Insurance.initColumn();
    var table = new BSTable(Insurance.id, "/insurance/list", defaultColunms);
    table.setPaginationType("client");
    Insurance.table = table.init();
});
